package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Book;
import db.MyHibernateSessionFactory;

//书籍DAO层类
public class BookDAO {

	public List<Book> queryBooksByName(String bookname) {
		return queryBookBySql("bookname='" + bookname + "'");
	}

	public List<Book> queryBooksByCategoryID(String categoryid) {
		return queryBookBySql("category='" + categoryid + "'");
	}

	public List<Book> queryBooksByAuthor(String author) {
		return queryBookBySql("author='" + author + "'");
	}

	public List<Book> queryBooksByPublishing(String publishing) {
		return queryBookBySql("publishing=" + publishing);
	}

	public List<Book> queryBookBySql(String sql) {
		Transaction transaction = null;
		List<Book> list = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory()
					.getCurrentSession();
			transaction = session.beginTransaction();// 事务开启
			Book book = new Book();
			Query query = session.createQuery("from Book where "+sql);
			list = query.list();
			transaction.commit();
			if(list.size() > 0){
				return list;
			}
			else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			transaction.commit();
			return list;
		}
	}

	public List<Book> queryBookBySQL(String sql) {
		Transaction transaction = null;
		List<Book> list = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory()
					.getCurrentSession();
			transaction = session.beginTransaction();// 事务开启
			Book book = new Book();
			Query query = session.createSQLQuery(sql).addEntity(Book.class);
			list = query.list();
			transaction.commit();
			if(list.size() > 0){
				return list;
			}
			else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			transaction.commit();
			return list;
		}
	}

	public boolean addBook(Book book) {
		Transaction transaction = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory()
					.getCurrentSession();
			transaction = session.beginTransaction();// 事务开启
			session.save(book);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.commit();
			return false;
		}
	}

	public boolean updateBook(Book book) {
		Transaction transaction = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory()
					.getCurrentSession();
			transaction = session.beginTransaction();// 事务开启
			session.update(book);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.commit();
			return false;
		}
	}

	public boolean deleteBook(String bookid) {
		Transaction transaction = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory()
					.getCurrentSession();
			transaction = session.beginTransaction();
			Query query = session.createQuery("from Book where bookid='"
					+ bookid + "'");
			List<Book> list = query.list();
			for (Book book : list) {
				session.delete(book);
			}
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.commit();
			return false;
		}
	}

}
